<?php
/** 订单  */
namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\service\Token as TokenService;
use app\api\validate\OrderPlace;
use app\api\service\Order as OrderService;
use app\api\model\Order as OrderModel;
use app\api\validate\PaginParameter;

class Order extends BaseController{

    //用户选择商品后向API提交包含所选择商品的相关信息
    //API接收信息后
    //有库存，把订单数据存入数据库中=下单成功，返回客户端消息，通知可以支付
    //调用支付接口，进行支付
    //进行库存量验证
    //服务器调用微信支付接口进行支付
    //微信返回我们一个支付的结果（异步）
    //成功：进行库存量检测
    //成功：进行库存量扣除，失败：返回支付失败结果
    
    protected $beforeActionList=[
        'checkExclusiveScope'=>['only'=>'placeOrder']
    ];

    //分页接口
    public function getSummarByUser($page=1,$size=20){
        (new PaginParameter())->goCheck();
        $uid=TokenService::getCurrentUid();
        $pagingOrders=OrderModel::getSummaryByUser($uid,$page,$size);
        if($pagingOrders->isEmpty()){
            return[
                'data'=>[],
                'current_page'=>$pagingOrders->getCurrentPage()
            ];
        }
        $data = $pagingOrders->hidden(['snap_items', 'snap_address'])
            ->toArray();
        return [
            'current_page' => $pagingOrders->currentPage(),
            'data' => $data
        ];
    }

    //订单接口
    public function placeOrder(){
        (new OrderPlace())->goCheck();
        $products=input('post.products/a');
        $uid=TokenService::getCurrentUid();

        $order=new OrderService();
        $status=$order->place($uid,$products);
        return $status;
    }
}